home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-04-06 | 2.1 KB | 71 lines | [TEXT/GEOL] |
- Item 9251348 5-April-90 13:14PDT
-
- From: D0532 Aidea Systems, Don Park,PRT
-
- To: CPLUS.DEV$ C++ Interest List--Developers
- CPLUS.APPLE$ C++ Interest List--Apple Employees
-
- Sub: Who's at home base?
-
- This is a follow up on my memo titled "Who's on first?".
-
- After reading the books like a hound dog and experimenting further, I have
- concluded the following:
-
- 1. One can not control order of static object instantiation in C++.
- 2. MPW Link instantiates static objects in the order of linking.
-
- The memos others sent me on the subject agrees.
-
- This implies that, in general, cout/cin/cerr can not be used in the
- constructors of static objects since cout/cin/cerr themselves are static
- objects in the stream library. For example:
-
- #include <stream.h>
-
- class Test
- {
- public:
- Test (void) { cout << "test constructor!\n"; }
- ~Test (void) { cout << "test destructor!\n"; }
- void Null (void) { cout << "test null!\n"; }
- };
-
- Test test;
-
- main ()
- {
- test.Null();
- }
-
- Above code generates following:
-
- test null!
-
- This is pretty sad for debugging. Under MPW however, one can get around this
- by linking CPlusLib.o before the above code. Automatically generated make
- files link CPlusLib.o AFTER user object files so one has to edit the make file
- manually to do this.
-
- After the fix, the output is:
-
- test constructor!
- test null!
- test destructor!
-
- Great for MPW, still sad for others.
-
- There is a way to get around all this with a little organizing. One can
- declare all static objects as members of a single static object, controlling
- the order of instantiation by the order of the members. While I don't know if
- this was the motive, same approach was used in Keith Gorlen's NIHCL (National
- Institutes of Health Class Library). NIHCL has at its root a class named NIHCL
- and all other classes, including Object class, are derived from it.
-
- So we go from "Who's on first?" to "Who's at home base?"
-
- Don Park
-
- P.S. Thanks to Joe MacDougald, Debra Orton, and Ed Ruder for replying.
-
-